﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using GreenyMeter.DOM.DataBaseAcces;

namespace GreenyMeter.DOM.Objets
{
    public class City
    {
        #region Atributes
        
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _countryCode;
        public string CountryCode
        {
            get { return _countryCode; }
            set { _countryCode = value; }
        }
        public City(int id, string name)
        {
            _id = id;
            _name = name;
        }
        public City()
        {
            _id = -1;
            _name = String.Empty;
        }
        public City(DataRow row)
        {
            if (!row.IsNull("ID"))
            {
                _id = Int32.Parse(row["ID"].ToString());
            }
            if (!row.IsNull("Name"))
            {
                _name = row["Name"].ToString();
            }
            if (!row.IsNull("CountryCode"))
            {
                _countryCode = row["CountryCode"].ToString();
            }
        }
        #endregion
        #region Methods
        /// <summary>
        /// Obteins a City by the city name
        /// </summary>
        /// <param name="cityId">city id </param>
        /// <returns></returns>
        public static City GetById(int cityId)
        {
            SqlDataAcces sqlQuery = new SqlDataAcces();
            List<SqlDaAccesParameters> sqlParams = new List<SqlDaAccesParameters>();
            sqlParams.Add(new SqlDaAccesParameters("@ID", cityId));
            DataTable dtCities = sqlQuery.GetDataTable("usp_CitySelectById", sqlParams);
            foreach (DataRow row in dtCities.Rows)
            {
                return new City(row);
            }
            return null;
        }
        public static City GetByCountryCode(string CountryCode)
        {
            SqlDataAcces sqlQuery = new SqlDataAcces();
            List<SqlDaAccesParameters> sqlParams = new List<SqlDaAccesParameters>();
            sqlParams.Add(new SqlDaAccesParameters("@CountryCode", CountryCode));
            DataTable dtCities = sqlQuery.GetDataTable("usp_CitySelectBycCountryCode", sqlParams);
            foreach (DataRow row in dtCities.Rows)
            {
                return new City(row);
            }
            return null;
        }
        /// <summary>
        /// Obteins all cities in the database
        /// </summary>
        /// <returns>List of cities registered in the database</returns>
        public static List<City> GetAllCities()
        {

            SqlDataAcces sqlQuery = new SqlDataAcces();
            City city;
            List<City> res = new List<City>();
            DataTable dtCities = sqlQuery.GetDataTable("usp_CitySelectAll", new List<SqlDaAccesParameters>());
            foreach (DataRow row in dtCities.Rows)
            {
                city = new City(Int32.Parse(row["CityID"].ToString()), row["CityName"].ToString());
                res.Add(city);
            }
            return res;

        }
        #endregion

    }
}